package com.zrgj.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zrgj.common.response.BaseResponse;
import com.zrgj.common.response.StatusCodeEnum;
import com.zrgj.domain.User;
import com.zrgj.domain.UserComments;
import com.zrgj.manager.service.IProductService;
import com.zrgj.manager.service.IUserCommentsService;
import com.zrgj.manager.service.IUserService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/comments")
public class CommentsController {
    @Reference
    private IUserService userService;
    @Reference
    private IUserCommentsService userCommentsService;
    @Reference
    private IProductService productService;

    @RequestMapping(value = "/getCommentsList")
    public BaseResponse getCommentsList(Integer productId){
        List<UserComments> commentsList = userCommentsService.selectByPid(productId);
        return new BaseResponse(StatusCodeEnum.SUCCESS,commentsList);
    }

    @RequestMapping(value = "/releaseComments")
    public BaseResponse releaseComments(String comments, String userName, Integer productId){

        User user = userService.findByName(userName);

        UserComments userComments = new UserComments();
        if (user != null) {
            userComments.setUserId(user.getUserId());
            userComments.setCommentsContent(comments);
            userComments.setCommentsTime(new Date());
            userComments.setProductId(productId);
            userCommentsService.insert(userComments);
            userComments.setUser(user);
        }else {
            return new BaseResponse(StatusCodeEnum.NOT_LOGIN);
        }

        return new BaseResponse(StatusCodeEnum.SUCCESS, userComments);
    }
}
